import AntModal from '@/components/pt/dialog/AntModal'
import { addAccessory } from '@/api/biz/bizOrder'
import { upload } from '@/api/system/upload'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import QRCode from 'qrcodejs2';
export default {
  name: 'CreateForm',

  components: {
    AntModal
  },
  data() {
    return {
      showProductModal: false,
      showPlanModal: false,
      activeKey: "1",
      uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload",
      // 设置上传的请求头部
      headers: { Authorization: 'Bearer ' + storage.get(ACCESS_TOKEN) },
      multiple: false,
      multipleDetail:false,
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      loading: false,
      disabled: true,
      total: 0,
      id: undefined,
      formTitle: '设置配件信息',
      // 表单参数
      form: {orderType:"1"},
      attachmentRefName: 'addUploaderFile', // 标志表单是否含有附件
      formId: '',
      currentType:'',//当前新增明细类型
      list: [],
      selectedRowKeys: [],
      selectedDetailRowKeys: [],
      selectedDetailRows:[],
      ids: [],
      fileList: []
    }
  },
  filters: {},
  created() { 
  },
  computed: {
    
  },
  watch: {},
  mounted() {},
  methods: {
    show(selectedRows,data){
      this.open = true
      this.$nextTick(()=>{
            document.getElementById("QRcodeContent").innerHTML = data;
            let new_str = document.getElementById("QRcodeContent").innerHTML; //获取指定打印区域
            //构建新网页(关键步骤,必须先构建新网页,在生成二维码,否则不能显示二维码)
            // document.body.innerHTML = new_str;
            document.getElementById("XQ").innerHTML = ""; //置空
              new QRCode(document.getElementById("XQ"), {
                text: "http://192.168.31.135:8080/biz/bizOrder/view/process?orderNumber="+selectedRows[0].orderNumber+"&tenantId="+selectedRows[0].tenantId,
                width: 250,
                height: 250,
                colorDark: "#000000",
                colorLight: "#ffffff",
              });
            setTimeout(()=>{
              this.$print(this.$refs.QRcodeContent)
              this.onClose()
            }, 500);
            // window.print(); //打印刚才新建的网页
            // window.location.reload();
            return ;
      })
    },
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      console.log('cancel')
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        remark: undefined,
        orderId:undefined
      }
      this.fileList = []
      this.selectedRowKeys = []
    },

    /** 新增按钮操作 */
    handleSetAccessory(row) {
      this.open = true
      this.form.saleOrderId=row.id;
      this.form.id=row.accessoryId
      this.form.remark=row.accessoryRemark;
      this.fileList=[];
      if(row.accessoryFileList) {
        row.accessoryFileList.split(',').forEach((item,index) => {
          const d = {
            uid: index,
            name: item,
            url: item,
            status: 'done'
          }
          // 文件列表
          this.fileList.push(d)
        })
      }

    },
    modify(){
      this.disabled = false
      
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      // this.disabled = true
      if(this.fileList.length==0) {
        this.$message.warn('请上传配件图片');
      }
      let filePaths="";
      this.fileList.forEach(file=>{
        if(filePaths.length>0) {
          filePaths+=",";
        }
        filePaths+=file.url;
      })
      let saveForm={orderId:this.form.saleOrderId,
      fileList:filePaths,
        id:this.form.id,
        remark:this.form.remark
      }
      addAccessory(saveForm).then(response=>{
        this.$message.success('保存成功', 3)
        this.open = false
        this.$emit('ok')
        this.$emit('close')
      })
    },
    handleChange(info,e) {
      const formData = new FormData();
      formData.append('file', info.file);
      upload(formData).then(response => {
        const d = {
          uid: info.file.uid,
          name: response.fileName,
          url: response.url,
          status: 'done'
        }
        this.fileList.push(d);
    })
    },
    handleRemove(info) {
      this.fileList = this.fileList.filter(function (item) {
        return item.uid != info.uid
      })
    }
  }
}
